<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Users extends Admin_Controller {

    function __construct() {
        parent::__construct();
        $this->load->model('user_m');
        /** 检查登陆 */
        if (!$this->auth->is_admin()) {
            show_message('非管理员或未登录', site_url('admin/login/do_login'));
        }
    }

    public function index($page = 1) {
        $data['title'] = '用户管理';
        $data['act'] = $this->uri->segment(3);
        //分页
        $limit = 10;
        $config['uri_segment'] = 4;
        $config['use_page_numbers'] = TRUE;
        $config['base_url'] = site_url('admin/users/index');
        $config['total_rows'] = $this->db->count_all('users');
        $config['per_page'] = $limit;
        $config['prev_link'] = '&larr;';
        $config['first_link'] = '首页';
        $config['last_link'] = '尾页';
        $config['prev_tag_open'] = '<li class=\'prev\'>';
        $config['prev_tag_close'] = '</li';
        $config['cur_tag_open'] = '<li class=\'active\'><span>';
        $config['cur_tag_close'] = '</span></li>';
        $config['num_tag_open'] = '<li>';
        $config['num_tag_close'] = '</li>';
        $config['next_link'] = '&rarr;';
        $config['next_tag_open'] = '<li class=\'next\'>';
        $config['next_tag_close'] = '</li>';
        $config['last_link'] = '尾页';
        $config['last_tag_open'] = '<li class=\'last\'>';
        $config['last_tag_close'] = '</li>';
        $config['num_links'] = 10;

        $this->load->library('pagination');
        $this->pagination->initialize($config);
        $this->load->model('xueyuan_m');
        $data['xueyuan'] = $this->xueyuan_m->getXueyuan4Group();
        $data['group_type'] = $this->xueyuan_m->getGroup4group();
//       
        $start = ($page - 1) * $limit;
        $data['pagination'] = $this->pagination->create_links();
        $data['post_user_name'] = $this->input->post('username');
        $data['post_gid'] = $this->input->post("gid");
        $data['post_xuehao'] = $this->input->post("xuehao");
        $data['post_xueyuan'] = $this->input->post("xueyuan");
        $where = array();
        if ($data['post_user_name']) {
            $where['username'] = $data['post_user_name'];
        }
        if ($data['post_gid']) {
            $where['gid'] = $data['post_gid'];
        }
        if ($data['post_xuehao']) {
            $where['xuehao'] = $data['post_xuehao'];
        }
        if ($data['post_xueyuan']) {
            $where['xueyuan'] = $data['post_xueyuan'];
        }

        $data['users'] = $this->user_m->searchUsers4Admin($where, $start, $limit);

        $data['csrf_name'] = $this->security->get_csrf_token_name();
        $data['csrf_token'] = $this->security->get_csrf_hash();
        $this->load->view('users', $data);
//        var_dump($data);
    }

    public function index1() {
        $data['title'] = '站点设置';
        if ($_POST) {
            $str = array(
                array('value' => $this->input->post('site_name'), 'id' => 1),
                array('value' => $this->input->post('welcome_tip'), 'id' => 2),
                array('value' => $this->input->post('short_intro'), 'id' => 3),
                array('value' => $this->input->post('show_captcha'), 'id' => 4),
                array('value' => $this->input->post('site_run'), 'id' => 5),
                array('value' => $this->input->post('site_stats'), 'id' => 6),
                array('value' => $this->input->post('site_keywords'), 'id' => 7),
                array('value' => $this->input->post('site_description'), 'id' => 8),
                array('value' => $this->input->post('reward_title'), 'id' => 9),
                array('value' => $this->input->post('per_page_num'), 'id' => 10)
            );
            $this->db->update_batch('settings', $str, 'id');
            show_message('网站设置更新成功', site_url('admin/site_settings'), 1);
        }
        $data['item'] = $this->db->get_where('settings', array('group_type' => 0))->result_array();
        $this->load->view('users', $data);
    }

    public function edit($uid) {
        $data['title'] = '修改用户信息';
        $data['user'] = $this->user_m->get_user_by_uid($uid);
        $this->load->model('xueyuan_m');
        $data['xueyuan'] = $this->xueyuan_m->getXueyuan4Group();
//        var_dump($data);die;
        $this->load->model('group_m');
        if ($_POST) {
            $group_info = $this->group_m->get_group_info($this->input->post('gid'));
            $str = array(
                'username' => $this->input->post('username'),
                'email' => $this->input->post('email'),
                //'password'=> md5($this->input->post('password')),
                'homepage' => $this->input->post('homepage'),
                'location' => $this->input->post('location'),
                'qq' => $this->input->post('qq'),
                'signature' => $this->input->post('signature'),
                'introduction' => $this->input->post('introduction'),
                'credit' => $this->input->post('credit'),
                'gid' => $this->input->post('gid'),
                'group_type' => @$group_info['group_type']
            );

            if ($this->input->post('password')) {
                $salt = get_salt();
                $str['password'] = password_dohash($this->input->post('password'), $salt);
                $str['salt'] = $salt;
            }
            if ($this->user_m->update_user($uid, $str)) {
                show_message('修改用户成功', site_url('admin/users/index'), 1);
            }
        }

        $this->load->model('group_m');
        $data['groups'] = $this->group_m->group_list();
        $data['group'] = $this->db->get_where('user_groups', array('gid' => $data['user']['gid']))->row_array();
        //加载form类，为调用错误函数,需view前加载
        $this->load->helper('form');
        $data['csrf_name'] = $this->security->get_csrf_token_name();
        $data['csrf_token'] = $this->security->get_csrf_hash();
        $this->load->view('user_edit', $data);
    }

    public function addteacher() {
        $data['title'] = '添加教师';
        $this->load->model('xueyuan_m');
        $data['xueyuan'] = $this->xueyuan_m->getXueyuan4Group();
//        var_dump($data);die;
        $this->load->model('group_m');
        if ($_POST) {
//            var_dump($_POST);
//            die;
            $this->config->load('userset'); //用户积分
            $password = $this->input->post('password');

            $salt = get_salt();

            $group_info = $this->group_m->get_group_info($this->input->post('gid'));
            $str = array(
                'username' => strip_tags($this->input->post('username')),
                'email' => $this->input->post('email'),
                'xueyuan' => ($this->input->post('xueyuan')),
                'shimingzhuangtai' => 2,
                'xuehao' => $this->input->post('xuehao'),
                'location' => $this->input->post('location'),
                'signature' => $this->input->post('signature'),
                'introduction' => $this->input->post('introduction'),
                'credit' => $this->input->post('credit'),
                'gid' => 4, //教师组
                'group_type' => 3,
                'password' => password_dohash($password, $salt),
                'salt' => $salt
            );

            if ($this->user_m->add($str)) {
                show_message('新增用户成功', site_url('admin/users/index'), 1);
            }
        }

        //加载form类，为调用错误函数,需view前加载
        $this->load->helper('form');
        $data['csrf_name'] = $this->security->get_csrf_token_name();
        $data['csrf_token'] = $this->security->get_csrf_hash();
        $this->load->view('user_addteacher', $data);
    }

    public function group($act, $gid = '') {
        $data['title'] = '用户组管理';
        $data['act'] = $this->uri->segment(3) . $this->uri->segment(4);
        $this->load->model('group_m');
        $data['group_list'] = $this->group_m->group_list();
        $data['group_info'] = $this->group_m->get_group_info($gid);
        $check_group = $this->group_m->check_group(@$_POST['group_name']);
        if ($act == 'add') {
            $data['title'] = '添加用户组';
            if (@$_POST['group_name']) {
                if ($check_group) {
                    show_message('用户组已存在', site_url('admin/users/group/add'));
                }
                $str = array(
                    'group_name' => $this->input->post('group_name', true),
                    'group_type' => 2
                );
                if ($this->db->insert('user_groups', $str)) {
                    show_message('添加用户组成功', site_url('admin/users/group/index'), 1);
                }
            }
        }
        if ($act == 'edit') {
            $data['title'] = '编辑用户组';
            if (@$_POST['commit_edit']) {
                if ($check_group) {
                    show_message('用户组已存在', site_url('admin/users/group/edit/' . $gid));
                }
                $str = array(
                    'group_name' => $this->input->post('group_name', true)
                );
                if ($this->db->where('gid', $gid)->update('user_groups', $str)) {
                    show_message('修改用户组成功', site_url('admin/users/group/index'), 1);
                }
            }
        }
        if ($act == 'del') {
            if (@$data['group_info']['group_type'] > 1) {
                if ($this->db->where('gid', $gid)->delete('user_groups')) {
                    show_message('删除用户组成功', site_url('admin/users/group/index'), 1);
                }
            } else {
                show_message('无法删除系统用户组', site_url('admin/users/group/index'));
            }
        }
        $data['csrf_name'] = $this->security->get_csrf_token_name();
        $data['csrf_token'] = $this->security->get_csrf_hash();
        $this->load->view('users', $data);
    }

    /**
     * 批量删除大三的用户
     */
    function delOlder() {
        //学号前缀 
        $xuehaoprev = date("Y", strtotime("-3 year"));

        $where = array(
            'group_type' => 2, //普通用户
            'xuehao like' => "$xuehaoprev%"
        );
        $list = $this->user_m->getListByCondition('users', $where, array(), 0, -1, "*");

        $this->load->model('cate_m');

        foreach ($list as $key => $value) {
            $uid = $value['uid'];
            $user = $this->user_m->get_user_by_uid($uid);
            if ($uid == 1) {
                continue;
            }
            if (!$user) {
                continue;
            } else {
                $this->db->set('value', 'value-1', false)->where('item', 'total_users')->update('site_stats');
                $this->db->set('value', 'value-' . @$user['topics'], false)->where('item', 'total_topics')->update('site_stats');
                $this->db->set('value', 'value-' . @$user['replies'], false)->where('item', 'total_comments')->update('site_stats');
                $this->user_m->del($uid);
                //更新栏目中的数据
                $nodes = $this->cate_m->get_node_ids();
                foreach ($nodes as $k => $v) {
                    $data[$k]['node_id'] = @$v['node_id'];
                    $data[$k]['listnum'] = $this->db->where('node_id', @$v['node_id'])->count_all_results('topics');
                }
                $this->db->update_batch('nodes', $data, 'node_id');
                if ($user['avatar'] != 'uploads/avatar/default/') {
                    @unlink(FCPATH . $user['avatar'] . 'big.png');
                    @unlink(FCPATH . $user['avatar'] . 'normal.png');
                    @unlink(FCPATH . $user['avatar'] . 'small.png');
                }
            }
        }
        show_message('删除用户成功', site_url('admin/users/index'), 1);
    }

    public function del() {
        $uid = (int) $this->uri->segment(4);
        $user = $this->user_m->get_user_by_uid($uid);
        if ($uid == 1) {
            show_message('管理员不能被删除', site_url('admin/users/index'));
            die;
        }
        if (!$user) {
            show_message('用户uid不能为空', site_url('admin/users/index'));
        } else {
            $this->db->set('value', 'value-1', false)->where('item', 'total_users')->update('site_stats');
            $this->db->set('value', 'value-' . @$user['topics'], false)->where('item', 'total_topics')->update('site_stats');
            $this->db->set('value', 'value-' . @$user['replies'], false)->where('item', 'total_comments')->update('site_stats');
            $this->user_m->del($uid);
            //更新栏目中的数据
            $this->load->model('cate_m');
            $nodes = $this->cate_m->get_node_ids();
            foreach ($nodes as $k => $v) {
                $data[$k]['node_id'] = @$v['node_id'];
                $data[$k]['listnum'] = $this->db->where('node_id', @$v['node_id'])->count_all_results('topics');
            }
            $this->db->update_batch('nodes', $data, 'node_id');
            if ($user['avatar'] != 'uploads/avatar/default/') {
                @unlink(FCPATH . $user['avatar'] . 'big.png');
                @unlink(FCPATH . $user['avatar'] . 'normal.png');
                @unlink(FCPATH . $user['avatar'] . 'small.png');
            }
            show_message('删除用户成功', site_url('admin/users/index'), 1);
        }
    }

    public function search() {
        //查找用户
//        $data['title'] = '用户搜索';
//        $data['act'] = $this->uri->segment(3);
//        if ($_POST) {
//            $user_name = $this->input->post('username');
//            $gid = $this->index->post("gid", 0);
//            $xuehao = $this->index->post("xuehao", 0);
//            $data['users'] = $this->user_m->searchUsers4Admin($this->input->post('username'));
//        }
//        $this->load->view('users', $data);
    }

}
